package zeno.project.springboot.controller.core;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import zeno.project.springboot.annotation.UnInterception;
import zeno.project.springboot.constant.SystemConstant;
import zeno.project.springboot.dao.SysUserMapper;
import zeno.project.springboot.entity.User;
import zeno.project.springboot.entity.base.JsonResult;
import zeno.project.springboot.entity.system.SysUser;
import zeno.project.springboot.service.UserService;
import zeno.project.springboot.service.impl.TestService;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @author ZhiYong
 * @ClassName TestController.java
 * @Description log日志打印学习
 * @createTime 2021/04/18 18:30:00
 */
@RestController
@RequestMapping("/test")
@Api(value = "log日志打印学习")
public class TestController {
    private final Logger logger = LoggerFactory.getLogger(TestController.class);
    @Autowired
    private TestService testService;
    @Autowired
    private UserService userService;
    @Autowired
    private SysUserMapper sysUserMapper;



    @GetMapping("/log")
    @ResponseBody
    @ApiOperation(value = "测试日志记录打印")
    public String testLog() {
        logger.debug("=====测试日志debug级别打印====");
        logger.info("======测试日志info级别打印=====");
        logger.error("=====测试日志error级别打印====");
        logger.warn("======测试日志warn级别打印=====");

        // 可以使用占位符打印出一些参数信息
        String str1 = "blog.itcodai.com";
        String str2 = "blog.csdn.net/eson_15";
        logger.info("======倪升武的个人博客：{}；倪升武的CSDN博客：{}", str1, str2);

        return testService.sayHello();
    }

    /**
     * 测试从缓存中获取类似于首页的数据
     */
    @GetMapping("/user")
    public User getUser(HttpServletRequest request) {
        ServletContext application = request.getServletContext();
        return (User) application.getAttribute(SystemConstant.USER_SESSION);
    }

    /**
     * 获取当前在线人数，该方法有bug
     * @param request request
     */
    @GetMapping("/total")
    public String getTotalUser(HttpServletRequest request) {
        Integer count = (Integer) request.getSession().getServletContext().getAttribute("count");
        return "当前在线人数：" + count;
    }

    /**
     * 获取当前在线用户数量并设置有效期
     */
    @GetMapping("/total2")
    public String getTotalUser(HttpServletRequest request, HttpServletResponse response) {
        Cookie cookie;
        try {
            // 把sessionId记录在浏览器中
            cookie = new Cookie("JSESSIONID", URLEncoder.encode(request.getSession().getId(), "utf-8"));
            cookie.setPath("/");
            //设置cookie有效期为2天，设置长一点
            cookie.setMaxAge( 48*60 * 60);
            response.addCookie(cookie);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Integer count = (Integer) request.getSession().getServletContext().getAttribute("count");
        return "当前在线人数：" + count;
    }

    /**
     * 获取请求中的信息
     */
    @GetMapping("/request")
    public Object getRequestInfo(HttpServletRequest request) {
        logger.info("requestListener中的初始化的name数据：{}", request.getAttribute("name"));
        return new JsonResult<>(userService.getUser2());
    }

    /**
     * 测试拦截器是否生效
     */
    @GetMapping("/interceptor")
    public String interceptor() {
        return "hello";
    }

    /**
     * 测试拦截器是否生效
     */
    @GetMapping("/no-interceptor")
    @UnInterception
    public String noInterceptor() {
        return "hello noInterceptor";
    }

    /**
     * 测试拦截器是否生效
     */
    @GetMapping("/sysUserMapper")
    public void sysUserMapper() {
        SysUser sysUser = new SysUser();
        sysUser.setAccount("test1");
        sysUser.setUsername("测试填充时间");
        sysUser.setPassword("123456");
        sysUserMapper.insert(sysUser);
    }

}
